package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.ActivityAddDto;
import com.frejoys.common.dto.admin.ActivityUpDto;
import com.frejoys.common.dto.admin.DelIdDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.ActivityPageVo;
import com.frejoys.common.vo.admin.AdminCategoryInfoVo;
import com.frejoys.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "活动类型", description = "活动类型")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminActivityController {

    private final ActivityService activityService;

    private final RedissonClient redissonClient;

    @Operation(summary = "后台查询活动类型列表")
    @PostMapping("/activity/pageList")
    public Result<IPage<ActivityPageVo>> pageList(@RequestBody @Validated PageDto dto) {
        return Result.success(activityService.pageList(dto));
    }

    @Operation(summary = "新增活动类型")
    @PostMapping("/activity/addInfo")
    public Result addInfo(@RequestBody @Validated ActivityAddDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.adActivityLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                activityService.addInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @Operation(summary = "编辑活动类型")
    @PostMapping("/activity/upInfo")
    public Result upInfo(@RequestBody @Validated ActivityUpDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upActivityLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                activityService.upInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @Operation(summary = "删除活动类型")
    @PostMapping("/activity/delInfo")
    public Result delInfo(@RequestBody @Validated DelIdDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upActivityLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                activityService.delInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }
}
